package com.qf.fmall2204.index.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2204.constants.Constants;
import com.qf.fmall2204.index.entity.Category;
import com.qf.fmall2204.index.entity.IndexImg;
import com.qf.fmall2204.index.service.ICategoryService;
import com.qf.fmall2204.index.service.IndexImgService;
import com.qf.fmall2204.product.entity.Product;
import com.qf.fmall2204.product.service.IProductService;
import com.qf.fmall2204.vo.ResultVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 轮播图  前端控制器
 * </p>
 *
 * @author dong
 * @since 2023-04-21
 */
@CrossOrigin // 跨域
@RestController
@RequestMapping("/index")
public class IndexImgController {

    @Autowired
    IndexImgService indexImgService;

    @Autowired
    ICategoryService categoryService;

    @Autowired
    IProductService productService;


    @GetMapping("/category-recommends")
    public ResultVo categoryRecommends() {

//        List<Category> categories = categoryService.categoryRecommends();
        List<Category> categories = categoryService.categoryRecommendsBetter();

        return new ResultVo(Constants.SUCCESS_CODE, "success", categories);

    }


    @GetMapping("/list-recommends")
    public ResultVo listRecommends() {

        List<Product> productList = productService.listRecommends();

        return new ResultVo(Constants.SUCCESS_CODE, "success", productList);

    }


    @GetMapping("/category-list")
    public ResultVo categoryList() {

        List<Category> categories = categoryService.categoryListBySql();

        return new ResultVo(Constants.SUCCESS_CODE, "success", categories);
    }


    @RequiresPermissions("index:get")
    @GetMapping("/indeximg")
    public ResultVo indeximg() {

        // SELECT * FROM index_img WHERE STATUS=1 ORDER BY seq;
        List<IndexImg> imgList = indexImgService
                .list(new QueryWrapper<IndexImg>().eq("status", 1).orderByAsc("seq"));

        // 返回json数据
        return new ResultVo(Constants.SUCCESS_CODE, "success", imgList);
    }

}
